package com.epam.cdp.jpa.dao;

import java.util.List;

import javax.ejb.Stateful;
import javax.persistence.Query;

import com.epam.cdp.jpa.model.Author;
import com.epam.cdp.jpa.model.Book;

@Stateful(name = "AuthorDAO")
public class AuthorDAO extends AbstractStatelessDAO<Author> {

    @Override
    public Class<Author> getEntityClass() {
	return Author.class;
    }

    @SuppressWarnings("unchecked")
    public List<Author> getByFirstNameFragment(String firstnamePart) {
	Query query = getEntityManager().createQuery("SELECT a FROM Author a WHERE a.firstName LIKE '" + firstnamePart + "%'", Author.class);
	return query.getResultList();
    }
    
    @SuppressWarnings("unchecked")
    public List<Book> getAllBooksFor(Author author) {
	return (List<Book>) getEntityManager().createNamedQuery("Author.allBooksForAuthor").setParameter("id", author.getId()).getResultList();
    }
}
